﻿using System;
using System.Linq;
using System.Web.Mvc;
using JStore.Core.Entities;
using JStore.Core.Enums;
using JStore.Tasks;
using JStore.Web.Controllers.MoneyLogs.ViewModels;
using SpeadFrm.Extensions;
using SpeadFrm.Mappers;
using SpeadFrm.MvcPager;
using SpeadFrm.Utility;

namespace JStore.Web.Controllers.MoneyLogs
{
    public class MoneyLogsController : BaseController
    {
        private readonly MoneyLogTask _moneyLogTask = new MoneyLogTask();
        private readonly WeekMoneyLogTask _weekMoneyLogTask = new WeekMoneyLogTask();
        private readonly CompanyMoneyLogTask _companyMoneyLogTask = new CompanyMoneyLogTask();

        public ActionResult UserList(int? moneyType, int pageIndex = 1, int pageSize = 10)
        {
            ViewData["MoneyType"] = moneyType;
            var pagedList = _moneyLogTask.GetPagedList(LogOnUserName, string.Empty, moneyType, pageIndex, pageSize);
            var items = pagedList.Select(EntityMapper.Map<MoneyLog, MoneyLogModel>).ToList();
            var model = new PagedList<MoneyLogModel>(items, pageIndex, pageSize, pagedList.TotalItemCount); 
            return View(model);
        }

        public ActionResult List(string userName, string sourceUserName, int? moneyType, int pageIndex = 1, int pageSize = 10)
        {
            var moneyTypeList = MoneyType.FosterMoney.ToSelectList();
            moneyTypeList.Insert(0, new SelectListItem { Text = "全部", Value = string.Empty });
            ViewData["moneyTypeList"] = moneyTypeList;

            var pagedList = _moneyLogTask.GetPagedList(userName, sourceUserName, moneyType, pageIndex, pageSize);
            var items = pagedList.Select(EntityMapper.Map<MoneyLog, MoneyLogModel>).ToList();
            var model = new PagedList<MoneyLogModel>(items, pageIndex, pageSize, pagedList.TotalItemCount);
            return View(model);
        }

        public ActionResult WeekList(int? weekMoneyLogId)
        {
            var now = DateTime.Now;
            var startDate = DateTimeHelper.GetWeekMonday(now);
            var endDate = DateTimeHelper.GetWeenSunday(now);

            var riseMoney = _moneyLogTask.GetSum(startDate, endDate);
            var realMoney = _companyMoneyLogTask.GetSum(startDate, endDate);

            var weekMoneyList = _weekMoneyLogTask.GetAll()
                .ToSelectList(c => c.Id.ToString(), c => string.Format("{0}-{1}", c.StartDate.ToString("yyyy年MM月dd日"), c.EndDate.ToString("yyyy年MM月dd日")));
            weekMoneyList.Insert(0, new SelectListItem { Text = "全部", Value = string.Empty });
            ViewData["WeekMoneyList"] = weekMoneyList;

            var list = _weekMoneyLogTask.GetList(weekMoneyLogId)
                .Select(EntityMapper.Map<WeekMoneyLog, WeekMoneyLogModel>)
                .ToList();
            list.Insert(0, new WeekMoneyLogModel
                         {
                             CreateDate = DateTime.Now,
                             EndDate = endDate,
                             StartDate = startDate,
                             RealMoney = realMoney,
                             RiseMoney = riseMoney,
                             Id = 0
                         });
            return View(list);
        }
         
        [HttpPost]
        public void Pass(int id)
        {
            _moneyLogTask.Pass(LogOnUserName, id);
        }
    }
}